package com.joinway.framework.support.security;

import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.joinway.framework.bean.security.annotation.PermissionValidate;
import com.joinway.framework.core.utils.AopHelper;
import com.joinway.framework.support.security.validator.IPermissionValidator;

public class PermissionAspect {
	private final static Logger log = LoggerFactory.getLogger(PermissionAspect.class);
	
	protected List<IPermissionValidator> validators;
	
	public Object doAround(ProceedingJoinPoint point) throws Throwable {
		
		PermissionValidate ann = AopHelper.getAnnotation(point, PermissionValidate.class, true);
		
		if(ann != null && CollectionUtils.isNotEmpty(validators)){
			for(IPermissionValidator validator : validators){
				validator.validate(point);
			}
		}
		
		return point.proceed();
	}

	public List<IPermissionValidator> getValidators() {
		return validators;
	}

	public void setValidators(List<IPermissionValidator> validators) {
		this.validators = validators;
	}
}
